<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人信息展示</title>
    <style>
        :root {
            --primary-color: #4361ee;
            --secondary-color: #3f37c9;
            --success-color: #4cc9f0;
            --error-color: #f72585;
            --bg-color: #f8f9fa;
            --text-color: #2b2d42;
            --light-text: #8d99ae;
            --border-color: #e9ecef;
            --card-bg: #ffffff;
            --gradient-start: #f7f7f7;
            --gradient-end: #e9ecef;
        }

        body {
            background: linear-gradient(135deg, var(--gradient-start), var(--gradient-end));
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0;
            padding: 20px;
        }

        .profile-container {
            max-width: 400px;
            width: 90%;
            padding: 2rem;
            background: var(--card-bg);
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            text-align: center;
            transition: transform 0.3s ease;
            position: relative;
            overflow: hidden;
            border: 1px solid rgba(0, 0, 0, 0.05);
        }

        .profile-container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 5px;
            background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
        }

        .profile-container:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
        }

        .profile-img {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            object-fit: cover;
            margin-bottom: 1.5rem;
            border: 4px solid var(--card-bg);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
            cursor: pointer;
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            padding: 3px;
        }

        .profile-img:hover {
            transform: scale(1.05) rotate(3deg);
            box-shadow: 0 8px 20px rgba(67, 97, 238, 0.3);
        }

        h3 {
            color: var(--primary-color);
            margin: 0 0 0.5rem 0;
            font-size: 1.8rem;
            font-weight: 600;
            letter-spacing: -0.5px;
        }

        .info-list {
            margin: 1.5rem 0;
            text-align: left;
            background: rgba(248, 249, 250, 0.5);
            border-radius: 12px;
            padding: 1rem;
            border: 1px solid var(--border-color);
        }

        .info-item {
            display: flex;
            justify-content: space-between;
            padding: 0.8rem 0;
            border-bottom: 1px solid var(--border-color);
        }

        .info-item:last-child {
            border-bottom: none;
        }

        .info-label {
            color: var(--light-text);
            font-size: 0.9rem;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            font-weight: 500;
        }

        .info-value {
            color: var(--text-color);
            font-weight: 500;
            font-size: 1rem;
        }

        .button-group {
            display: flex;
            justify-content: center;
            gap: 1rem;
            margin-top: 2rem;
            flex-wrap: wrap;
        }

        .input-file {
            display: none;
        }

        .btn-upload, .btn-logout {
            cursor: pointer;
            padding: 0.8rem 1.8rem;
            border: none;
            border-radius: 8px;
            font-weight: 600;
            transition: all 0.3s ease;
            flex: 1;
            min-width: 140px;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 0.5rem;
            letter-spacing: 0.5px;
            text-transform: uppercase;
            font-size: 0.9rem;
            position: relative;
            overflow: hidden;
        }

        .btn-upload {
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
            color: white;
            box-shadow: 0 4px 6px rgba(67, 97, 238, 0.2);
        }

        .btn-upload:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 15px rgba(67, 97, 238, 0.3);
        }

        .btn-upload::after {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
            transition: all 0.5s ease;
        }

        .btn-upload:hover::after {
            left: 100%;
        }

        .btn-logout {
            background: linear-gradient(135deg, var(--error-color) 0%, #b5179e 100%);
            color: white;
            box-shadow: 0 4px 6px rgba(247, 37, 133, 0.2);
        }

        .btn-logout:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 15px rgba(247, 37, 133, 0.3);
        }

        .btn-logout::after {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
            transition: all 0.5s ease;
        }

        .btn-logout:hover::after {
            left: 100%;
        }

        /* 优化后的模态框样式 */
        .modal {
            backdrop-filter: blur(8px);
            background: rgba(0, 0, 0, 0.4);
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            justify-content: center;
            align-items: center;
            z-index: 1000;
            animation: fadeIn 0.3s ease;
        }

        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }

        .modal-content {
            background: white;
            border-radius: 16px;
            padding: 2rem;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            transform: scale(0.9);
            transition: all 0.3s cubic-bezier(0.68, -0.55, 0.27, 1.55);
            width: 90%;
            max-width: 450px;
            border-top: 5px solid var(--primary-color);
            position: relative;
            overflow: hidden;
        }

        .modal-content.show {
            transform: scale(1);
        }

        .modal-content::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(135deg, rgba(67, 97, 238, 0.1), rgba(63, 55, 201, 0.1));
            z-index: -1;
        }

        .status-icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 1rem;
            background: white;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
        }

        .status-icon.success-icon {
            background: var(--success-color);
            color: white;
        }

        .status-icon.error-icon {
            background: var(--error-color);
            color: white;
        }

        .modal-title {
            font-size: 1.5rem;
            color: var(--primary-color);
            margin: 0 0 1rem 0;
            font-weight: 600;
        }

        .modal-message {
            font-size: 1.1rem;
            color: var(--text-color);
            margin: 1rem 0;
            line-height: 1.6;
        }

        .modal-btn {
            background: var(--primary-color);
            padding: 0.8rem 2rem;
            border-radius: 6px;
            transition: all 0.3s ease;
            font-weight: 600;
            border: none;
            color: white;
            cursor: pointer;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            font-size: 0.9rem;
            box-shadow: 0 3px 8px rgba(67, 97, 238, 0.3);
        }

        .modal-btn:hover {
            background: var(--secondary-color);
            transform: translateY(-2px);
            box-shadow: 0 5px 12px rgba(67, 97, 238, 0.4);
        }

        @media (max-width: 480px) {
            .profile-container {
                padding: 1.5rem;
                margin: 1rem;
            }

            .profile-img {
                width: 100px;
                height: 100px;
            }

            .button-group {
                flex-direction: column;
                gap: 0.75rem;
            }

            .btn-upload, .btn-logout {
                width: 100%;
                padding: 0.8rem;
                font-size: 0.8rem;
            }

            .modal-content {
                padding: 1.5rem;
            }

            .modal-title {
                font-size: 1.3rem;
            }

            .modal-message {
                font-size: 1rem;
            }
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<div class="profile-container">
    <img id="profileImage" class="profile-img" src="/page/images/default_avatar.png" alt="User Avatar">
    <h3 id="username">加载中...</h3>

    <div class="info-list">
        <div class="info-item">
            <span class="info-label">邮箱</span>
            <span class="info-value" id="email">加载中...</span>
        </div>

        <div class="info-item">
            <span class="info-label">性别</span>
            <span class="info-value" id="gender">加载中...</span>
        </div>

        <div class="info-item">
            <span class="info-label">年龄</span>
            <span class="info-value" id="age">加载中...</span>
        </div>

        <div class="info-item">
            <span class="info-label">手机号</span>
            <span class="info-value" id="phone">加载中...</span>
        </div>
    </div>

    <div class="button-group">
        <input type="file" id="fileInput" class="input-file" accept="image/*">
        <label for="fileInput" class="btn-upload">上传头像</label>
        <button id="logoutButton" class="btn-logout" onclick="logout()">登出</button>
    </div>
</div>

<script>
    function fetchUserInfo() {
        axios.get('http://localhost:8081/user/info')
            .then(response => {
                const user = response.data.data;
                document.getElementById('username').textContent = user.username || '未知用户名';
                document.getElementById('email').textContent = user.email || '未绑定邮箱';
                document.getElementById('gender').textContent = user.gender === 'male' ? '男'
                    : user.gender === 'female' ? '女'
                        : '其他';
                document.getElementById('age').textContent = user.age || '未填写';
                document.getElementById('phone').textContent = user.phone || '未绑定';
                if (user.image) {
                    document.getElementById('profileImage').src = 'http://localhost:8081/common/download?name=' + user.image;
                } else {
                    document.getElementById('profileImage').src = '/page/images/default_avatar.png';
                }
            })
            .catch(error => {
                console.error('获取用户信息出错:', error);
                showModal('获取用户信息失败，请刷新重试', 'error');
                document.getElementById('username').textContent = '加载失败';
                document.getElementById('email').textContent = '加载失败';
                document.getElementById('gender').textContent = '加载失败';
                document.getElementById('age').textContent = '加载失败';
                document.getElementById('phone').textContent = '加载失败';
                document.getElementById('profileImage').src = '/images/default_avatar.png';
            });
    }

    document.getElementById('fileInput').addEventListener('change', function (event) {
        const file = event.target.files[0];
        if (file) {
            const formData = new FormData();
            formData.append('file', file);

            axios.post('http://localhost:8081/common/upload', formData, {
                headers: { 'Content-Type': 'multipart/form-data' }
            })
                .then(response => {
                    showModal('头像上传成功！', 'success');
                    const imageName = response.data.data;
                    const newImageUrl = 'http://localhost:8081/common/download?name=' + imageName;
                    document.getElementById('profileImage').src = newImageUrl;

                    return axios.put('http://localhost:8081/user/image', {
                        image: imageName
                    }, {
                        headers: { 'Content-Type': 'application/json' }
                    });
                })
                .then(response => {
                    console.log('头像数据已成功发送到 user/image:', response.data);
                })
                .catch(error => {
                    console.error('上传头像或发送头像数据出错:', error);
                    showModal('上传头像成功但是更新头像失败', 'error');
                });
        }
    });

    function logout() {
        axios.post('http://localhost:8081/user/logout')
            .then(response => {
                const res = response.data;
                if (res.code === 1) {
                    showModal('登出成功', 'success');
                    localStorage.removeItem('token');
                    sessionStorage.removeItem('token');
                    window.location.href = 'login.html';
                } else {
                    showModal('登出失败', 'error');
                }
            })
            .catch(error => {
                console.error('登出请求出错：', error);
                showModal('登出失败', 'error');
            });
    }

    window.onload = fetchUserInfo;
</script>

<div id="customModal" class="modal">
    <div class="modal-content">
        <div class="modal-header">
            <div id="modalStatusIcon" class="status-icon"></div>
            <h3 id="modalTitle" class="modal-title"></h3>
        </div>
        <div class="modal-body">
            <p id="modalMessage" class="modal-message"></p>
        </div>
        <div class="modal-footer">
            <button id="modalConfirmBtn" class="modal-btn">确定</button>
        </div>
    </div>
</div>

<script>
    function showModal(message, type = 'info', title = '提示') {
        const modal = document.getElementById('customModal');
        const statusIcon = document.getElementById('modalStatusIcon');
        const modalTitle = document.getElementById('modalTitle');
        const modalMessage = document.getElementById('modalMessage');
        const confirmBtn = document.getElementById('modalConfirmBtn');

        // 重置样式
        statusIcon.className = 'status-icon';
        modalTitle.className = 'modal-title';
        modalMessage.className = 'modal-message';

        if(type === 'success') {
            statusIcon.classList.add('success-icon');
            statusIcon.innerHTML = '✓';
            modalTitle.textContent = title || '成功';
            modal.style.backgroundColor = 'rgba(76, 201, 240, 0.3)';
        } else if(type === 'error') {
            statusIcon.classList.add('error-icon');
            statusIcon.innerHTML = '✗';
            modalTitle.textContent = title || '错误';
            modal.style.backgroundColor = 'rgba(247, 37, 133, 0.3)';
        } else {
            modalTitle.textContent = title || '提示';
            modal.style.backgroundColor = 'rgba(0, 0, 0, 0.4)';
        }

        modalMessage.textContent = message;
        modal.style.display = 'flex';

        let autoCloseTimer = setTimeout(() => {
            hideModal();
        }, 5000);

        confirmBtn.onclick = () => {
            clearTimeout(autoCloseTimer);
            hideModal();
        };

        modal.onclick = (e) => {
            if(e.target === modal) {
                clearTimeout(autoCloseTimer);
                hideModal();
            }
        };
    }

    function hideModal() {
        document.getElementById('customModal').style.display = 'none';
    }
</script>
</body>
</html>